package metier;

import java.util.Date;

import main.Commande;
import main.Vente;

public class Production {
	
	private final int prod_jour = 1075;
	
	private Date date_debut_prod = null;
	private Date date_fin_prod = null;
	private Date date_en_cours = null;
	
	private int quantite_to_prod_1;
	private int quantite_to_prod_2;
	
	private int type_sauc_1;
	private int type_sauc_2;
	
	
	private double conso_sel = 0.0025;
	private double conso_ail = 0.1;
	private double conso_epices = 0.02;
	private double conso_noix = 0.5;
	
	/**
	 * 
	 * @param date_debut
	 * @param date_fin
	 * @param type_sauc // 1 = sec ou 2 = noix
	 */
	public Production(Date date_debut, Date date_fin, int type_sauc) {
		this.date_debut_prod = date_debut;
		this.date_fin_prod = date_fin;
		this.type_sauc_1 = type_sauc;
		
		//#TODO tofix
	//	this.hachage(type_sauc_1);
	}

	/**
	 * 
	 * @param date_debut
	 * @param quantité 1
	 * @param type_sauc 1// 1 = sec ou 2 = noix
	 * @param quantité 2
	 * @param type_sauc 2// 1 = sec ou 2 = noix
	 */
	public Production(Date date_debut, int quantite_toprod_1, int type_sauc_1, int quantite_toprod_2, int type_sauc_2) {
		this.date_debut_prod = date_debut;
		this.quantite_to_prod_1 = quantite_toprod_1;
		this.type_sauc_1 = type_sauc_1;
		this.quantite_to_prod_2 = quantite_toprod_2;
		this.type_sauc_2 = type_sauc_2;
		
		this.hachage(type_sauc_1, type_sauc_2);
	}

	
	private void hachage(int type_1, int type_2)
	{
		
		if ((this.quantite_to_prod_1 != 0) && (this.quantite_to_prod_2 != 0) && (this.date_debut_prod != null))
		{
			this.date_en_cours = this.date_debut_prod;
			do{
				if (this.quantite_to_prod_1 > 0)
					this.date_en_cours = this.creation_saucisson_sec(this.date_en_cours, this.quantite_to_prod_1);
				if (this.quantite_to_prod_2 > 0)
					this.date_en_cours = this.creation_saucisson_sec_noix(this.date_en_cours, this.quantite_to_prod_2);
				
//				System.out.println("quantite 2 : " + this.quantite_to_prod_2);
//				System.out.println("quantite 1 : " + this.quantite_to_prod_1);
								
			}while((quantite_to_prod_1 > 0) || (quantite_to_prod_2 > 0));
		}
		else
			System.out.println("PAS DE FABRIQUATION");
		
		/*if ((this.date_debut_prod != null) && (this.date_fin_prod != null))
			this.creation_saucisson_sec(date_debut_prod, date_fin_prod);
		else if ((this.quantite_to_prod_1 != 0) && (this.quantite_to_prod_2 != 0) && (this.date_debut_prod != null))
			this.creation_saucisson(this.date_debut_prod, this.quantite_to_prod_1, type_1, this.quantite_to_prod_2, type_2);
		else
			System.out.println("PAS DE FABRIQUATION");
		*/
		Stock.getInstance().affichage_stock(this.date_en_cours);

		Stock.getInstance().check_stock(date_fin_prod);
	}
	
	
	private void rea_boyaux()
	{
		//#TODO traitement		
	}
	
	private int sechage(int date_entree)
	{
		int date_sortie = 0;
		//#TODO traitement		
		
		return date_sortie;
	}
	
	
	private int creation_saucisson_sec(Date dateDebut, Date dateFin)
	{
		int Y = 0;
		int M = 0;
		int D = 0;
		if (dateDebut.before(dateFin))
		{
			Y = dateFin.getYear() - dateDebut.getYear();
			M = dateFin.getMonth() - dateDebut.getMonth();
			D = dateFin.getDate() - dateDebut.getDate();
		}
		
		System.out.println("nb année : " + Y);
		System.out.println("nb mois : " + M);
		System.out.println("nb jours : " + D);
		
		return 0;
	}
	
	
	private Date creation_saucisson_sec(Date _dateProd, int quantite_prod)
	{
		boolean sel = true;
		boolean ail = true;
		boolean epices = true;
		int date_prec = 0;
		Date dateProd = _dateProd;
		
		System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +  " : Lancement de la production de saucisson sec.");
		
		int jour_prod = quantite_prod/this.prod_jour;
		
		for (int i = 0; i < 31; i++)
		{
			Commande.getInstance().doCommande(dateProd);
			
			if ((Stock.getInstance().getAil() > 0) && (Stock.getInstance().getAil() >= (this.conso_ail*this.prod_jour)))
				ail = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +  " : Plus d'ail.");
				ail = false;
			}
			
			if ((Stock.getInstance().getSel()> 0) &&(Stock.getInstance().getSel() >= (this.conso_sel*this.prod_jour)))
				sel = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +   " : Plus de sel.");
				sel = false;
			}
			
			if ((Stock.getInstance().getEpices()> 0) &&(Stock.getInstance().getEpices() >= (this.conso_epices*this.prod_jour)))
				epices = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() + " : Plus d'epices.");
				epices = false;
			}
			
						
			if (ail && sel && epices)
			{
				Stock.getInstance().setAil(Stock.getInstance().getAil() - (this.conso_ail*this.prod_jour));
				Stock.getInstance().setSel(Stock.getInstance().getSel() - (this.conso_sel*this.prod_jour));
				Stock.getInstance().setEpices(Stock.getInstance().getEpices() - (this.conso_epices*this.prod_jour));
				Stock.getInstance().setSauc_sec(Stock.getInstance().getSauc_sec() + this.prod_jour);
			}
			this.quantite_to_prod_1 = this.quantite_to_prod_1 - this.prod_jour;
			if (this.quantite_to_prod_1 < 0)
				this.quantite_to_prod_1 = 0;
			
			Stock.getInstance().check_stock(dateProd);
			dateProd.setDate(dateProd.getDate()+1);
			
			if (i%5 == 0)
				dateProd.setDate(dateProd.getDate()+2);
			
			if (date_prec == 0)
				date_prec = dateProd.getMonth();
			
			if (date_prec != dateProd.getMonth())
			{
				Vente.getInstance().vendre_sec(dateProd);
				break;
			}
		}
		
		return dateProd;
	}
	
	private Date creation_saucisson_sec_noix(Date _dateProd, int quantite_prod)
	{
		boolean sel = true;
		boolean ail = true;
		boolean epices = true;
		boolean noix = true;
		int date_prec = 0;
		Date dateProd = _dateProd;
		
		System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +  " : Lancement de la production de saucisson sec aux noix.");
		
		int jour_prod = quantite_prod/this.prod_jour;
		
		for (int i = 0; i < 31; i++)
		{
			Commande.getInstance().doCommande(dateProd);
			
			if ((Stock.getInstance().getAil() > 0) && (Stock.getInstance().getAil() >= (this.conso_ail*this.prod_jour)))
				ail = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +  " : Plus d'ail.");
				ail = false;
			}
			
			if ((Stock.getInstance().getSel()> 0) &&(Stock.getInstance().getSel() >= (this.conso_sel*this.prod_jour)))
				sel = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +   " : Plus de sel.");
				sel = false;
			}
			
			if ((Stock.getInstance().getEpices()> 0) &&(Stock.getInstance().getEpices() >= (this.conso_epices*this.prod_jour)))
				epices = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() + " : Plus d'epices.");
				epices = false;
			}
			
			if ((Stock.getInstance().getNoix()> 0) &&(Stock.getInstance().getNoix() >= (this.conso_noix*this.prod_jour)))
				noix = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() + " : Plus de noix.");
				noix = false;
			}
			
						
			if (ail && sel && epices && noix)
			{
				Stock.getInstance().setAil(Stock.getInstance().getAil() - (this.conso_ail*this.prod_jour));
				Stock.getInstance().setSel(Stock.getInstance().getSel() - (this.conso_sel*this.prod_jour));
				Stock.getInstance().setEpices(Stock.getInstance().getEpices() - (this.conso_epices*this.prod_jour));
				Stock.getInstance().setNoix(Stock.getInstance().getNoix() - (this.conso_noix*this.prod_jour));
				Stock.getInstance().setSauc_sec_noix(Stock.getInstance().getSauc_sec_noix() + this.prod_jour);
			}
			
			this.quantite_to_prod_2 = this.quantite_to_prod_2 - this.prod_jour;
			if (this.quantite_to_prod_2 < 0)
				this.quantite_to_prod_2 = 0;
			
			Stock.getInstance().check_stock(dateProd);
			dateProd.setDate(dateProd.getDate()+1);
			
			if (i%5 == 0)
				dateProd.setDate(dateProd.getDate()+2);
			
			if (date_prec == 0)
				date_prec = dateProd.getMonth();
			
			if (date_prec != dateProd.getMonth())
			{
				Vente.getInstance().vendre_noix(dateProd);
				break;
			}
		}
		
		return dateProd;
	}
	
	
	
	private int creation_saucisson(Date dateProd, int quantite_prod_1, int type_1, int quantite_prod_2, int type_2)
	{
		
		int type_actu = type_1;

		int Y = 0;
		int M = 0;
		int D = 0;
		
//		double qte_sel = this.conso_sel * quantite_prod;
//		double qte_ail = this.conso_ail * quantite_prod;
//		double qte_epices = this.conso_epices * quantite_prod;
//		double qte_noix = this.conso_noix * quantite_prod;

		
		boolean sel = true;
		boolean ail = true;
		boolean epices = true;
		boolean noix = false;

		if (type_actu == type_1)
		{
			if (type_actu == 1)
				System.out.println("\n***Lancement de la production de " + quantite_prod_1 +" saucisson sec ****\n");
			else
				System.out.println("\n***Lancement de la production de " + quantite_prod_1 +" saucisson sec au noix****\n");
		}
		else if (type_actu == type_2)
		{
			if (type_actu == 1)
				System.out.println("\n***Lancement de la production de " + quantite_prod_2 +" saucisson sec ****\n");
			else
				System.out.println("\n***Lancement de la production de " + quantite_prod_2 +" saucisson sec au noix****\n");
		}
		
		int jour_prod_1 = quantite_prod_1/this.prod_jour;
		int jour_prod_2 = quantite_prod_2/this.prod_jour;
		
		for (int i = 0; i < jour_prod_1 + jour_prod_2; i++)
		{
			Commande.getInstance().doCommande(dateProd);
			
			if ((Stock.getInstance().getAil() > 0) && (Stock.getInstance().getAil() >= (this.conso_ail*this.prod_jour)))
				ail = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +  " : Plus d'ail.");
				ail = false;
			}
			
			if ((Stock.getInstance().getSel()> 0) &&(Stock.getInstance().getSel() >= (this.conso_sel*this.prod_jour)))
				sel = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() +   " : Plus de sel.");
				sel = false;
			}
			
			if ((Stock.getInstance().getEpices()> 0) &&(Stock.getInstance().getEpices() >= (this.conso_epices*this.prod_jour)))
				epices = true;
			else
			{
				System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() + " : Plus d'epices.");
				epices = false;
			}
			
			if (type_actu == 2)
			{
				if ((Stock.getInstance().getNoix()> 0) &&(Stock.getInstance().getNoix() >= (this.conso_noix*this.prod_jour)))
					noix = true;
				else
				{
					System.out.println(dateProd.getDate() + "/" + dateProd.getMonth() + "/" + dateProd.getYear() + " : Plus de noix.");
					noix = false;
				}	
			}
			
				if (ail && sel && epices)
				{
					Stock.getInstance().setAil(Stock.getInstance().getAil() - (this.conso_ail*this.prod_jour));
					Stock.getInstance().setSel(Stock.getInstance().getSel() - (this.conso_sel*this.prod_jour));
					Stock.getInstance().setEpices(Stock.getInstance().getEpices() - (this.conso_epices*this.prod_jour));
					if (type_actu == 1)
						Stock.getInstance().setSauc_sec(Stock.getInstance().getSauc_sec() + this.prod_jour);
				}
			
			if (type_actu == 2)
			{
				if (noix)
				{
					Stock.getInstance().setNoix(Stock.getInstance().getNoix() - (this.conso_noix*this.prod_jour));
					Stock.getInstance().setSauc_sec_noix(Stock.getInstance().getSauc_sec_noix() + this.prod_jour);
				}
			}
			
			Stock.getInstance().check_stock(dateProd);
			dateProd.setDate(dateProd.getDate()+1);
			
			if (i%5 == 0)
				dateProd.setDate(dateProd.getDate()+2);
			
			if (type_actu == type_1)
			{
				quantite_prod_1 -= this.prod_jour;
				if (i%30 == 0)
				{
					if (quantite_prod_2 > 0)
						type_actu = type_2;
				}
				
				if (quantite_prod_1 == 0)
					type_actu = type_2;
			}
			else if (type_actu == type_2)
			{
				quantite_prod_2 -= this.prod_jour;
				
				if (i%30 == 0)
				{
					if (quantite_prod_1 > 0)
						type_actu = type_1;
				}
				
				if (quantite_prod_2 == 0)
					type_actu = type_1;
			}
			if (i%30 == 0)
			{
				if (type_actu == 1)
					Vente.getInstance().vendre_noix(dateProd);
				else if (type_actu == 2)
					Vente.getInstance().vendre_sec(dateProd);
			}
			
			
		}
		
		Stock.getInstance().affichage_stock(dateProd);

	//	System.out.println("\nNombre de jours de production : " + (double)(quantite_prod/this.prod_jour));
		
		return 0;
	}
}
